package com.roam.service.impl;

import com.roam.result.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
/*
该工具用于存储前端传来的图片文件
 */
public class UploadTX {

    private String uploadDir="/root/roam-touring-backend/avatars/";
    public String upload(MultipartFile file,int userID){
        try {
            // 获取文件名并确保唯一性
            String timeStamp = String.valueOf(System.currentTimeMillis());
            String fileName = userID + "_" + timeStamp+".jpg";

            // 将文件保存到指定目录
            Path filePath = Paths.get(uploadDir + fileName);
            Files.copy(file.getInputStream(), filePath);

            // 返回文件的URL
            String fileUrl = "http://8.134.215.31:11112/image/" + fileName;
            return fileUrl;
        } catch (IOException ex) {
            ex.printStackTrace();
            String error="保存失败";
            return error;
        }
    }
}
